package org.yenn.pattern.aliyun.behavioral.memento;

import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;

/**
 * 步骤 4
 * 使用 CareTaker 和 Originator 对象。
 */
@Slf4j
public class MementoPatternDemo {

    Originator originator = new Originator();
    CareTaker careTaker = new CareTaker();

    @Before
    public void setUp() throws Exception {
        originator.setState("State #1");
        originator.setState("State #2");
        careTaker.add(originator.saveStateToMemento());
        originator.setState("State #3");
        careTaker.add(originator.saveStateToMemento());
        originator.setState("State #4");

        log.info("originator={}",originator);
        log.info("careTaker={}",careTaker);

    }

    /**
     * 步骤 5
     * 验证输出。
     *
     * Current State: State #4
     * First saved State: State #2
     * Second saved State: State #3
     */
    @Test
    public void get() {

        System.out.println("Current State: " + originator.getState());
        originator.getStateFromMemento(careTaker.get(0));
        System.out.println("First saved State: " + originator.getState());
        originator.getStateFromMemento(careTaker.get(1));
        System.out.println("Second saved State: " + originator.getState());
    }
}